//
//  DDMainTabBar.m
//  DDMusic
//
//  Created by rimi on 16/11/8.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDMainTabBar.h"
#import "DDPublicMacro.h"
#import "UILabel+DDUILabelCategory.h"
#import "UIButton+DDUIButtonCategory.h"
#import "UIImage+DDUiimageCategory.h"
#import <Masonry.h>
#import "DDPlayMusicViewController.h"
@interface DDMainTabBar()
@property (nonatomic,strong) UIButton* nextMusicBtn;//下一首歌曲
@property (nonatomic,strong) UIButton* showOrHiddenLyricBtn;//显示或者隐藏歌词按钮

@end
@implementation DDMainTabBar

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
    }
    return self;
}
#pragma mark  布局UI
- (void)layoutUI{
//背景图片
    UIImageView* bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"navigationBarImage"]];
    [self addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
        make.top.equalTo(@0);
    }];
    
//playingMusicBtn
    self.playingMusicBtn = [[UIButton alloc]init];
    [self addSubview:self.playingMusicBtn];
    [self.playingMusicBtn addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.playingMusicBtn setBackgroundImage:[UIImage imageNamed:@"Music.jpg"] forState:(UIControlStateNormal)];
//playingMusicSlider
    self.playingMusicSlider = [[DDMusicProgressView alloc]init];
    self.playingMusicSlider.progressStyle = MusicProgressOnlySliderStyle;
    UIImage* circleImage = [UIImage imageNamed:@"circle"];
    [self.playingMusicSlider refreshUI:circleImage minimumTrackTintColor:[UIColor whiteColor] maximumTrackTintColor:[UIColor lightGrayColor]];
    [self addSubview:self.playingMusicSlider];

//playingMusicNameLabel
    self.playingMusicNameLabel = [UILabel dd_CreatLabelWithTitle:@"呆呆音乐" font:15 TitleColor:[UIColor whiteColor] TextAlignment:(NSTextAlignmentLeft)];
    [self addSubview:self.playingMusicNameLabel];
    
//playingMusicSingerLabel
    self.playingMusicSingerLabel = [UILabel dd_CreatLabelWithTitle:@"呆呆" font:12 TitleColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentLeft];
    [self addSubview:self.playingMusicSingerLabel];
//statrOrStopBtn
    self.statrOrStopBtn = [UIButton dd_creatButtonTitle:nil buttonType:(UIButtonTypeCustom) font:0 titieColor:nil bgColor:nil nomalImage:nil bgImage:@"goOn"];
    [self.statrOrStopBtn setBackgroundImage:[UIImage imageNamed:@"stop"] forState:(UIControlStateSelected)];
    [self.statrOrStopBtn addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:(UIControlEventTouchUpInside)];

    
    [self addSubview:self.statrOrStopBtn];

//nextMusicBtn
    self.nextMusicBtn = [UIButton dd_creatButtonTitle:nil buttonType:UIButtonTypeCustom  font:0 titieColor:nil bgColor:nil nomalImage:nil bgImage:@"next"];
    [self.nextMusicBtn addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.nextMusicBtn];
//showOrHiddenLyricBtn
    self.showOrHiddenLyricBtn = [UIButton dd_creatButtonTitle:nil buttonType:UIButtonTypeCustom font:0 titieColor:nil bgColor:nil nomalImage:nil bgImage:@"lyric"];
    [self.showOrHiddenLyricBtn addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.showOrHiddenLyricBtn];
}
- (void)layoutSubviews{
#pragma mark 移除掉tabBar的子视图UITabBarButton来解决被UITabBarButton遮挡而不能点击的问题
    for (int i = 0; i<self.subviews.count; i++) {
        if ([self.subviews[i] isMemberOfClass:NSClassFromString(@"UITabBarButton")]) {
            [self.subviews[i] removeFromSuperview];
        }        
    }
    self.frame = CGRectMake(0, SCREEN_SIZE.height-60, SCREEN_SIZE.width, 60);
//playingMusicBtn
    self.playingMusicBtn.frame = CGRectMake(16, -5, self.frame.size.height, self.frame.size.height);
    self.playingMusicBtn.layer.cornerRadius = self.frame.size.height/2;
    self.playingMusicBtn.layer.masksToBounds = YES;
    [self addIconViewAnimate];
//playingMusicSlider
    self.playingMusicSlider.frame = CGRectMake(self.playingMusicBtn.frame.size.width+self.playingMusicBtn.frame.origin.x+8, 4,SCREEN_SIZE.width- self.playingMusicBtn.frame.size.width-self.playingMusicBtn.frame.origin.x-8-10 ,10);
    [self.playingMusicSlider.musicSider addTarget:self action:@selector(sliderMusicProgress) forControlEvents:(UIControlEventValueChanged)];
//playingMusicNameLabel
    self.playingMusicNameLabel.frame = CGRectMake(self.playingMusicSlider.frame.origin.x, self.playingMusicSlider.frame.size.height+self.playingMusicSlider.frame.origin.y+2,0.48*SCREEN_SIZE.width, 20);

//playingMusicSingerLabel
    self.playingMusicSingerLabel.frame = CGRectMake(self.playingMusicSlider.frame.origin.x, self.playingMusicNameLabel.frame.origin.y+self.playingMusicNameLabel.frame.size.height, 0.48*SCREEN_SIZE.width, 20);
//statrOrStopBtn
    self.statrOrStopBtn.frame = CGRectMake(SCREEN_SIZE.width-140, 24, 25, 25);
//nextMusicBtn
    self.nextMusicBtn.frame = CGRectMake(self.statrOrStopBtn.frame.origin.x+self.statrOrStopBtn.frame.size.width+25, 24, 25, 25);
//showOrHiddenLyricBtn
    self.showOrHiddenLyricBtn.frame = CGRectMake(self.nextMusicBtn.frame.origin.x+self.nextMusicBtn.frame.size.width+25, 24, 25, 25);
  
}
#pragma mark 逻辑操作
#pragma mark 标签栏按钮的回调方法
- (void)tabBarButtonClicked:(UIButton*)button{
    if (button == self.nextMusicBtn) {
        //下一首按钮
        [[DDPlayMusicViewController sharedDDPlayMusicViewController] playNextOrUpBtnClicked:[DDPlayMusicViewController sharedDDPlayMusicViewController].nextBtn];
    }else if(button == self.showOrHiddenLyricBtn){
    //歌词按钮
        self.showOrHiddenLyricBtn.selected = !self.showOrHiddenLyricBtn.selected;
        if (self.showOrHiddenLyricBtn.selected ==YES) {
            
        }else{
        
        }
        
    }else if (button == self.statrOrStopBtn){
    //开始或暂停歌曲按钮
        self.statrOrStopBtn.selected = !self.statrOrStopBtn.selected;
        if (self.statrOrStopBtn.selected == YES) {
          
        }else{
            
        }
        [[DDPlayMusicViewController sharedDDPlayMusicViewController] playNextOrUpBtnClicked:[DDPlayMusicViewController  sharedDDPlayMusicViewController].stopOrPauseBtn];
    }else{
    //歌曲专辑按钮
        [[NSNotificationCenter defaultCenter]postNotificationName:@"DDMainTabBarPlayingMusicBtnClicked" object:@{ISPLAYNEWMUSIC:@"NO"}];
    }

}
#pragma mark 滑条的回调方法
- (void)sliderMusicProgress{
    if ([DDPlayMusicViewController sharedDDPlayMusicViewController].stopOrPauseBtn.selected ==YES) {
        [[DDPlayMusicViewController sharedDDPlayMusicViewController].player seekToTime:CMTimeMake(self.playingMusicSlider.musicSider.value*[[DDPlayMusicViewController sharedDDPlayMusicViewController].currentPlayModel.seconds doubleValue], 1)];
        
    }
}
#pragma 歌曲按钮旋转的方法
- (void)addIconViewAnimate
{
    
    // 1.创建基本动画
    CABasicAnimation *rotationAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    // 2.给动画设置一些属性
    rotationAnim.fromValue = @(0);
    rotationAnim.toValue = @(M_PI * 2);
    rotationAnim.repeatCount = NSIntegerMax;
    rotationAnim.duration = 35;
    // 3.将动画添加到iconView的layer上面
    [self.playingMusicBtn.layer addAnimation:rotationAnim forKey:nil];

    
    
}
#pragma mark 单例
static  DDMainTabBar* instance = nil;
+ (instancetype)sharedDDMainTabBar{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DDMainTabBar alloc]init];
        [instance layoutUI];
    });
    return instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    @synchronized (self) {
        if (!instance){
            instance = [super allocWithZone:zone];
        }
    }
    return instance;
    
}
- (id)copyWithZone:(NSZone *)zone{
    return self;
}

@end
